package com.leadcloud.nim.jsbridge;

import android.os.Bundle;
import android.support.v4.widget.SwipeRefreshLayout;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.widget.AbsListView;
import android.widget.AdapterView;
import android.widget.ImageView;
import android.widget.TextView;

import com.leadcloud.nim.R;
import com.leadcloud.nim.config.preference.Preferences;
import com.netease.nim.uikit.api.NimUIKit;
import com.netease.nim.uikit.api.model.contact.ContactChangedObserver;
import com.netease.nim.uikit.api.model.main.LoginSyncDataStatusObserver;
import com.netease.nim.uikit.api.model.main.OnlineStateChangeObserver;
import com.netease.nim.uikit.api.model.user.UserInfoObserver;
import com.netease.nim.uikit.business.contact.ContactsFragment;
import com.netease.nim.uikit.business.contact.core.item.AbsContactItem;
import com.netease.nim.uikit.business.contact.core.item.ContactItem;
import com.netease.nim.uikit.business.contact.core.item.ItemTypes;
import com.netease.nim.uikit.business.contact.core.model.ContactDataAdapter;
import com.netease.nim.uikit.business.contact.core.provider.ContactDataProvider;
import com.netease.nim.uikit.business.contact.core.query.IContactDataProvider;
import com.netease.nim.uikit.business.contact.core.viewholder.LabelHolder;
import com.netease.nim.uikit.business.contact.core.viewholder.OnlineStateContactHolder;
import com.netease.nim.uikit.common.MyConst;
import com.netease.nim.uikit.common.fragment.TFragment;
import com.netease.nim.uikit.common.ui.liv.LetterIndexView;
import com.netease.nim.uikit.common.util.log.LogUtil;
import com.netease.nim.uikit.impl.NimUIKitImpl;
import com.netease.nim.uikit.impl.cache.UIKitLogTag;
import com.netease.nimlib.jsbridge.util.WebViewConfig;
import com.netease.nimlib.sdk.Observer;

import java.util.ArrayList;
import java.util.List;
import java.util.Set;

import me.everything.android.ui.overscroll.OverScrollDecoratorHelper;


/**
 * 网站留言
 */
public class SiteFragment extends TFragment {

    private SwipeRefreshLayout sr_pull_fresh;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        return inflater.inflate(R.layout.web_content_frg, container, false);
    }

    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);

        // 界面初始化
        findViews();
        initWebView();
        initData();
    }

    private void initData() {
        //enterpriseId：企业id
        //userId：用户id
        //userName：用户名
        //currentUrl：初次访问网址
        //proj：访客id
        //source：来源（1:访客历史,2:网站留言,3:直接创建,4:聊天）
        //sourceName 表单名（留言表的名字 ps:如果不是从网站留言录入，则不传）
        //sourceId：留言id（那条记录的id ps:如果不是从网站留言录入，则不传）

        String text = "?enterpriseId=10166&userId=8&userName=yuanzhi";
        StringBuilder sb = new StringBuilder();
        sb.append("?enterpriseId=")
                .append(Preferences.getOrgId())
                .append("&userId=")
                .append(Preferences.getUserId())
                .append("&userName=")
                .append(Preferences.getUserName())
                .append("&password=")
                .append(Preferences.getPwd());

        String url = MyConst.SITE_MSG_URL+sb.toString();
        //String url = MyConst.ADD_INFO_URL+text;
        LogUtil.ui(url);
        Preferences.syncCookie(webView,getContext());
        webView.loadUrl(url);
    }



    private void initWebView() {
        WebSettings settings = webView.getSettings();
        WebViewConfig.setWebSettings(getActivity(), settings, getActivity().getApplicationInfo().dataDir);
        WebViewConfig.removeJavascriptInterfaces(webView);
        WebViewConfig.setWebViewAllowDebug(false);
        WebViewConfig.setAcceptThirdPartyCookies(webView);
        settings.setDomStorageEnabled(true);// 打开本地缓存提供JS调用,至关重要
        settings.setAppCacheMaxSize(1024 * 1024 * 8);// 实现8倍缓存
        settings.setAllowFileAccess(true);
        settings.setAppCacheEnabled(true);
        String appCachePath = getActivity().getApplication().getCacheDir().getAbsolutePath();
        settings.setAppCachePath(appCachePath);
        settings.setDatabaseEnabled(true);
    }

    @Override
    public void onDestroy() {
        super.onDestroy();

    }

    private WebView webView;
    private void findViews() {
        webView = findView(R.id.webView);
        sr_pull_fresh = findView(R.id.sr_pull_fresh);
        sr_pull_fresh.setColorSchemeResources(R.color.primary_color);
        sr_pull_fresh.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                if(webView!=null){
                    webView.reload();
                }
                sr_pull_fresh.setRefreshing(false);
            }
        });
    }

}
